iT邦幫忙

1

利用Wscript顯示GUI 選單並執行命令,類似.BAT

  • 分享至 

  • xImage
  •  

還是習慣Windows舊版控制台元件,又背不住指令,寫一個vbs
#mycontrol.vbs

'Multi button GUI in Wscript,利用wscript顯示視窗功能表,並執行命令,取代command/choice
'Ref:https://stackoverflow.com/questions/21428596/multi-button-gui-vbsscript

const FormText ="開啟控制台"   				'功能表標題
const LabelText="請點選按鈕"					'第一行文字
const MenuCols =4       '功能表欄數
const MenuCount=5       '功能表數量,第0個=exit
const ButtonHeight=25	'高度,預設25        
const ButtonWidth =75   '寬度,4個中文字約75 

MenuTXT=Array("離開","控制台","網路連線","防火牆","電源選項")
MenuCMD=Array("","control","control Ncpa.cpl","control firewall.cpl","control powercfg.cpl")

'以下程式不需修改
Dim fso,objShell
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

'製作暫存ps1
const TemporaryFolder = 2
Dim sTempPath,MenuFile,pos
sTempPath = fso.GetSpecialFolder(TemporaryFolder)
MenuFile = WScript.scriptname
pos=InStrRev(MenuFile,".")
MenuFile=sTempPath+"\"+Left(MenuFile,pos-1)+".ps1"

call MakeTempPS1(MenuFile)

'WScript.Quit 0

'開始顯示選單/點選=>StdOut回傳=>清除CR(CHR13)及LF(CHR10)
Set exec = objShell.Exec("powershell -executionpolicy bypass -noninteractive -window hidden -file "&MenuFile)
exec.StdIn.Close()
str=exec.StdOut.ReadAll()  
str=REPLACE(str,vbcr,"")  
str=REPLACE(str,vblf,"")

'執行完後刪除暫存檔
'WScript.Echo "["+str+"]"
If (len(str) >0 ) Then objShell.Run str,4
fso.DeleteFile(MenuFile)

Set exec = Nothing
Set fso=Nothing
Set objShell = Nothing
WScript.Quit 0

'製作選單PS1
Sub MakeTempPS1(MenuFile)
   'WScript.Echo "["+MenuFile+"]"
   Set ps=fso.OpenTextFile(MenuFile,2,True)  '2:Create :read 8:append
   ps.Write "#"&Now&" Create Tempfile from "&WScript.ScriptFullName&chr(13)
   dim FormW,FormH,BTX,BTY   '表單大小W*H,按鈕位置XY
   FormW=25+(ButtonWidth +5)*MenuCols                    '25:左邊界 5:按鈕左邊界
   FormH=70+(ButtonHeight+5)*(int(MenuCount/MenuCols)+1) '70:上邊界 5:按鈕上邊界
   
   ps.write "Add-Type -AssemblyName System.Windows.Forms" &chr(13)&_
"$Form = New-Object system.Windows.Forms.Form" &chr(13)&_
"$Form.Font = New-Object System.Drawing.Font(""Tahoma"",10,[System.Drawing.FontStyle]::Bold)"&chr(13)&_
"$Form.Text = """&FormText&""""&chr(13)&_
"$Form.Width = "&FormW&chr(13)&_
"$Form.Height = "&FormH&chr(13)&_
"$Form.ControlBox = $False"&chr(13)&_
"$Form.StartPosition = ""CenterScreen"""&chr(13)
    ps.write chr(13)&_
"$Label = New-Object System.Windows.Forms.Label"&chr(13)&_
"$Label.Text = """&LabelText&""""&chr(13)&_
"$Label.AutoSize = $True"&chr(13)&_
"$Form.Controls.Add($Label)"&chr(13)
   '把第0個調到最後一個
   Dim item
   For i=0 to MenuCount-1   'Array重0起算
       item=i+1 : If item = MenuCount Then item=0   '按鈕左上開始點
	   BTX=5 +(ButtonWidth +5)*(i mod MenuCols)     'X=左邊界+(按鈕寬+按鈕左邊界)*按鈕cols
	   BTY=25+(ButtonHeight+10)*int(i/MenuCols)     'Y=上邊界+(按鈕高+按鈕上邊界)*按鈕rows
       ps.write chr(13)&_
"$Button"&i+1&" = new-object System.Windows.Forms.Button"&chr(13)&_
"$Button"&i+1&".Location = new-object System.Drawing.Size("&BTX&","&BTY&")"&chr(13)&_
"$Button"&i+1&".Size = new-object System.Drawing.Size("&ButtonWidth&","&ButtonHeight&")"&chr(13)&_
"$Button"&i+1&".Text = """ &MenuTXT(item)&""""&chr(13)&_
"$Button"&i+1&".Add_Click({Write-Host """&MenuCMD(item)&""";$Form.Close()})"&chr(13)&_
"$Form.Controls.Add($Button"&i+1&")"&chr(13)
   Next
   ps.write chr(13)&"$Form.ShowDialog() | Out-Null"&chr(13)&"Exit 0"
 
   ps.Close
   Set ps=nothing
End Sub

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言